from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from webselenium0617.po.add_member import AddMemberPage

#通讯录页面
from webselenium0617.po.basepage import BasePage


class ContactPage(BasePage):
    _ADDMEMBER = (By.CSS_SELECTOR, ".ww_operationBar .js_add_member")
    _NAMES = (By.CSS_SELECTOR,".member_colRight_memberTable_td:nth-child(2)")


    #定义点击添加成员操作
    def click_add_member(self):
        # 优化重复代码，将浏览器复用在basepage中定义
        # opt = webdriver.ChromeOptions()
        # opt.debugger_address = "127.0.0.1:9222"
        # self.driver = webdriver.Chrome(options=opt)
        # self.driver.implicitly_wait(5)

        ele = (By.CSS_SELECTOR, ".ww_operationBar .js_add_member")
        # WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(ele))
        # self.driver.find_element_by_css_selector(".ww_operationBar .js_add_member").click()
        # 优化代码，使用在basepage中封装的方法
        self.wait_for_click(self._ADDMEMBER)
        #点击后，跳转到通讯录页面,return的是一个页面对象，相当于页面跳转
        # self.driver是从basepage中继承过来
        return AddMemberPage(self.driver)
    #用于断言，获取人员信息
    def get_member_name(self):
        # opt = webdriver.ChromeOptions()
        # opt.debugger_address = "127.0.0.1:9222"
        # self.driver = webdriver.Chrome(options=opt)
        #定义空的列表，用于返回名字
        name_list = []
        # elements = self.driver.find_elements_by_css_selector(".member_colRight_memberTable_td:nth-child(2)")
        elements = self.finds(*self._NAMES)

        for value in elements:
            name_list.append(value.get_attribute("title"))
        return name_list